/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.Categoria;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;

/**
 *
 * @author Piter
 */
@ManagedBean
@SessionScoped
public class CategoryBean {
    @EJB
    private CategoriaBlLocal categoriaBl;
    private List<Categoria> lista;
    private Categoria category;
    private String accion;

    /**
     * Creates a new instance of Category
     */
    public CategoryBean() {
        category = new Categoria();
    }
    public List<Categoria> getLista(){
        lista=categoriaBl.getAll();
        return lista;
        
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
    public void prepararNuevo(ActionEvent evt){
        category = new Categoria();
        accion="nuevo";
        
    }
    public String procesarAccion(){
    if (accion.equalsIgnoreCase("Nuevo")) {
            categoriaBl.Register(category);
        } else if (accion.equalsIgnoreCase("Editar")) {
            categoriaBl.modify(category);
        }else if (accion.equalsIgnoreCase("Eliminar")){
             categoriaBl.delete(category);
        }

        accion = "";
        return "Categoria";
    
}
    public void prepararEditar(ActionEvent evt){
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category = lista.get(index);
        accion="editar";
    }
    public void eliminar(ActionEvent evt){
        accion="eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category = lista.get(index);
        
    }
    public String marrirris() {
        String h = "quiero ir a chapala";
        if (accion.equalsIgnoreCase("Nuevo")) {
            h = "Categoria";
        } else if (accion.equalsIgnoreCase("Editar")) {
            h = "CategoriaCrearEditar";
        } else if (accion.equalsIgnoreCase("Eliminar")) {
            h = "Categoria";
        }

        accion = "";
        return h;
    }
}